<?php
/**
 * UserController class file.
 *
 * @author Zhang Jinlong <466028373@qq.com>
 * @link http://www.ijianzhi.cn
 * @copyright Copyright &copy; 2013 DianJin Soft
 */

class UserController extends Controller
{
	/**
	 * @var string the default layout for the controller view. Defaults to '//layouts/column1',
	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
	 */
    public $layout = '//layouts/user';

    public function actionIndex()
    {
        $this->render('index');
    }

    /**
     * 基本信息
     */
    public function actionBase()
    {
        $model = UserMainModel::model()->findByPk(Yii::app()->user->uid);

        if (isset($_POST['UserMainModel']))
        {
            $model->attributes = $_POST['UserMainModel'];

            if ($model->validate() && $model->save(FALSE))
            {
                Helper::showMessage(Yii::t('app', 'Update your base info succeed.'), $this->createUrl('contact'));
            }
        }

        $this->render('base',array('model' => $model));
    }

    /**
     * 联系方式
     */
    public function actionContact()
    {
        $model = UserContactModel::model()->findByPk(Yii::app()->user->uid);

        if (isset($_POST['UserContactModel']))
        {
            $model->attributes = $_POST['UserContactModel'];

            if ($model->phoneHead || $model->phoneBody || $model->phoneFoot)
            {
                $model->phone = $model->phoneHead.'-'.$model->phoneBody
                    .(!empty($model->phoneFoot) ? '-'.$model->phoneFoot : '');
            }
            else
            {
                $model->phone = '';
            }

            if ($model->validate() && $model->save(FALSE))
            {
                Helper::showMessage(Yii::t('app', 'Update your contact info succeed.'), $this->createUrl('contact'));
            }
        }

        list($model->phoneHead, $model->phoneBody, $model->phoneFoot) = explode('-', $model->phone);

        $this->render('contact',array('model'=>$model));
    }
}